//
//  NewfeatureCollectionViewController.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/12.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewfeatureCollectionViewController: UICollectionViewController {

    init(){
    
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumLineSpacing = 0
        
        layout.minimumInteritemSpacing = 0
        
        layout.itemSize = CGSizeMake(screen_Width, screen_Height)
        
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout:layout)
        
        collectionView?.pagingEnabled = true
        
        collectionView?.showsVerticalScrollIndicator = false
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        collectionView?.bounces = false
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(CSYCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return featureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CSYCollectionViewCell
    
        // Configure the cell
        
        cell.backgroundColor = UIColor.redColor()
    
        cell.index = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //print("-------->\(indexPath.item)")
        
        if let cell = collectionView.visibleCells().last as? CSYCollectionViewCell {
        
            let index : NSInteger = (collectionView.indexPathForCell(cell)?.row)!
            
            //print("INDEX :: \(index)")
            
            collectionView.subviews.last
            
            if index == featureImageCount - 1{
                
                cell.startAnimation()
                
            }
        }
    }
    
    

//    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        
//        let page : NSInteger = NSInteger(scrollView.contentOffset.x / screen_Width)
//        
//        print(page)
//        
//        if page == featureImageCount - 1 {
//        
//            if let cell = collectionView?.visibleCells().last as? CSYCollectionViewCell {
//                
//                cell.startAnimation()
//                
//            }
//            
//        }
//        
//        
//    }

}

class CSYCollectionViewCell : UICollectionViewCell{

    
    var index : NSInteger = 0{
    
        didSet{
        
            startButton.hidden = true
            
            newFeatureImage.image = UIImage(named: "new_feature_\(index + 1)")
            
        }
        
    }
    
    func startAnimation (){
     
//        self.startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.0, options: [], animations: { () -> Void in
            
            self.startButton.hidden = false
            
            self.startButton.transform = CGAffineTransformMakeScale(1,1)
            
            }) { (_) -> Void in
                
                print("欢迎页载入完成")
                
        }
        
    }
    
    func setUpUI(){
    
        contentView.addSubview(newFeatureImage)
        
        contentView.addSubview(startButton)
        
        newFeatureImage.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
            
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(contentView.snp_bottom).offset(-screen_Height * 0.3)
            
            make.centerX.equalTo(contentView.snp_centerX).offset(0)
            
        }
        
        self.startButton.hidden = true
    
        self.startButton.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    @objc func clickStartButton(){
    
        NSNotificationCenter.defaultCenter().postNotificationName(APPDELEGATESHOULDSWITCHROOTVIEWCONTROLLER, object: "abc")
    
    }
    
    override init(frame: CGRect) {
     
        super.init(frame: frame)
        
        setUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    lazy var newFeatureImage : UIImageView = UIImageView()
    
    lazy var startButton : UIButton = {
    
        let btn = UIButton()
        
        btn.setTitle("开始体验", forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    
        return btn
    }()

}





